package com.sch.www.oauth.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.ValidationError;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;

import java.util.Map;


public class ResponseBody<T> {

    //成功
    public static final String SUCCESS_CODE = "10000";
    //失败
    public static final String ERROR_CODE = "20000";

    private String code = SUCCESS_CODE;
    private StringBuffer msg;
    private T data;


    public static <T> ResponseBody<T> newInstance() {
        ResponseBody resultBean = new ResponseBody();
        return resultBean;
    }

    public static <T> ResponseEntity<ResponseBody<T>> switchTo(ResponseEntity<T> responseEntity) {
        ResponseBody responseBody;
        if (!(responseEntity.getBody() instanceof ResponseBody)) {
            responseBody = newInstance();
            if (responseEntity.getStatusCode() == HttpStatus.OK) {
                responseBody.setSuccess().setData(responseEntity.getBody());
            } else {
                responseBody.setError().setData(responseEntity.getBody());
                if (responseEntity.getBody() instanceof Map) {
                    /**
                     * {
                     *     "timestamp": "2019-06-17T08:06:24.390+0000",
                     *     "status": 500,
                     *     "error": "Internal Server Error",
                     *     "message": "400 Bad Request",
                     *     "path": "/auth/v1/pcAdmin/oauth/token"
                     * }
                     */
                    responseBody.setMsg((String) ((Map) responseEntity.getBody()).get("message"));
                }
            }
        } else {
            responseBody = (ResponseBody) responseEntity.getBody();
        }
        return responseBody.createResponseEntity(responseEntity.getHeaders(), responseEntity.getStatusCode());

    }


    //=========================
    //       code
    //=========================

    public ResponseBody<T> setSuccess() {
        return setCode(SUCCESS_CODE);
    }

    public ResponseBody<T> setError() {
        return setCode(ERROR_CODE);
    }

    public String getCode() {
        return code;
    }

    public ResponseBody<T> setCode(String code) {
        this.code = code;
        return this;
    }


    //=========================
    //       msg
    //=========================

    public ResponseBody<T> appendMsg(String proPostSplit, String msg) {
        return appendMsg(proPostSplit, msg, proPostSplit);
    }

    public ResponseBody<T> appendMsg(String proSplit, String msg, String postSplit) {
        this.msg.append(proSplit);
        appendMsg(msg);
        this.msg.append(postSplit);
        return this;

    }

    public ResponseBody<T> appendMsg(String msg) {
        if (this.msg == null) {
            this.msg = new StringBuffer();
        }
        this.msg.append(msg);
        return this;
    }

    public ResponseBody<T> appendMsg(ComplexResult complexResult) {
        for (ValidationError error : complexResult.getErrors()) {
            appendMsg("<p>", error.getErrorMsg(), "</p>");
        }
        return this;
    }

    public ResponseBody<T> setMsg(String msg) {
        this.msg = new StringBuffer(msg);
        return this;
    }

    public String getMsg() {
        if (msg == null) {
            return "";
        } else {
            return msg.toString();
        }

    }

    //=========================
    //       data
    //=========================
    public T getData() {
        return data;
    }

    public ResponseBody<T> setData(T data) {
        this.data = data;
        return this;
    }

    //=========================
    //  createResponseEntity
    //=========================

    public ResponseEntity<ResponseBody<T>> createResponseEntity() {
        return createResponseEntity(HttpStatus.OK);
    }


    public ResponseEntity<ResponseBody<T>> createResponseEntity(HttpStatus status) {
        return createResponseEntity(null, status);
    }

    public ResponseEntity<ResponseBody<T>> createResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) {
        ResponseEntity<ResponseBody<T>> responseEntity = new ResponseEntity(this, headers, status);
        return responseEntity;
    }

}
